﻿/**
 * Example Model Class - just a basic framework for this class
 *
 * The Model of the MVC Triad 
 *
 * @author  	robert turrall
 * @date	   	31.12.2008
 * @license: 	This particular file may be used freely. It serves simply as an example.
 * 				Of course, a credit is always nice ;-)
 *
 * @version	1.0
 */
package { 

	// please note that it's not really good practice to use the default package (i.e. none). 
	// You should normally save your classes in a package which corresponds to your application
	
	import net.turrall.mvc.*;
	//import your other package classes as required here
	import flash.net.*;
	import flash.events.*;
	import flash.utils.*;
	
	class ExampleModel extends AbstractModel {
		
		// define your various classes used to hold and manipulate data
		private var _config:Object;
		
		/**
		 * define event/message constants
		 */
		public static const MODEL_INITIALISE:String = "ModelInitialise";
		public static const MODEL_INITERROR:String = "ModelInitError";
		
		/**
		 * constructor
		 *
		 */
		public function ExampleModel() {
			
			init();
		
		}
		
		/**
		 * getter for config
		 */
		public function get config():Object { return _config; } 
		
		
		/**
		 * initialise the model, loading data and creating 
		 * initial instances of all required classes
		 *
		 */
		private function init():void {
			
			// load your XML or text config file here and capture success or failure
								
			// example of how to dispatch an error event should the load of your config fail:
			// dispatchEvent(new CustomEvent(Model.MODEL_INITERROR, true, false, null));
			// or succeed:
			// dispatchEvent(new CustomEvent(Model.MODEL_INITIALISE, true, false, obj));  // where obj contains initial values
						
			
		}
	
		
		/**
		 * myFunction():void
		 *
		 * various public functions will be declared here to be initiated by the controller
		 * the results of calculations or data manipulation performed by these methods will
		 * be passed back to the view in the form of events and accompanying data - invoked
		 * by calling onUpdate() with a parameter of an object containing the data results
		 */
		public function myFunction(val:Number):void {
			
			// do whatever
			
			var obj:Object = new Object();
			obj.property = "something";
			// fill your object with results
			
			// calling onUpdate will invoke the method defined in AbstractModel, creating and 
			// dispatching a CustomEvent which will be received by the view
			onUpdate(obj);
		}		
	}
}
